﻿using System.Collections.Generic;
using System.Data.Linq;
using Dogshow.Model;
using DogshowRepository;
using System.Linq;

namespace Dogshow.Linq2SQLRepository
{
    public class KorisniciRepository : IKorisniciRepository
    {
        public int Sacuvaj(Korisnik entity)
        {
            using (var ctx = new DogshowModel())
            {
                var existing = ctx.Korisnicis.SingleOrDefault(x => x.KorisnickoIme == entity.KorisnickoIme);
                if (existing == null)
                {
                    ctx.Korisnicis.InsertOnSubmit(ModelMapper.Map(entity));
                }
                else
                {
                    var postojecaLozinka = existing.Lozinka;
                    if (entity is KorisnikKinoloskoDrustvo)
                    {
                        ModelMapper.Map((KorisnikKinoloskoDrustvo)entity, existing);
                    } else
                    {
                        ModelMapper.Map(entity, existing);    
                    }
                    if (entity.Lozinka == null)
                    {
                        existing.Lozinka = postojecaLozinka;
                    }
                }
                ctx.SubmitChanges();
            }
            return 0;
        }

        public IEnumerable<Korisnik> VratiSve()
        {
            using (var ctx = new DogshowModel())
            {
                return ctx.Korisnicis.Select(ModelMapper.Map).ToArray();
            }
        }

        public Korisnik VratiPoId(int id)
        {
            throw new System.NotImplementedException();
        }

        public Korisnik VratiPoKorisnickomImenu(string korisnickoIme)
        {
            using (var ctx = new DogshowModel())
            {
                return VratiPoKorisnickomImenu(ctx, korisnickoIme);
            }
        }

        private Korisnik VratiPoKorisnickomImenu(DogshowModel model, string korisnickoIme)
        {
            var dlo = new DataLoadOptions();
            dlo.LoadWith<Korisnici>(x => x.Kinolozi);
            model.LoadOptions = dlo;
            var korisnik = model.Korisnicis.SingleOrDefault(x => x.KorisnickoIme == korisnickoIme);
            if (korisnik == null) return null;
            return ModelMapper.Map(korisnik);
        }

        public void Obrisi(Korisnik korisnik)
        {
            using (var model = new DogshowModel())
            {
                model.ExecuteCommand("DELETE FROM Korisnici WHERE KorisnickoIme={0}", korisnik.KorisnickoIme);
            }
        }

        public bool PostojiKorisnik(string korisnickoIme)
        {
            using (var model = new DogshowModel())
            {
                return model.Korisnicis.Any(x => x.KorisnickoIme == korisnickoIme);
            }
        }

        public Korisnik VratiKorinsikaKinologa(int idKinologa)
        {
            using (var model = new DogshowModel())
            {
                var korisnik = model.Korisnicis.SingleOrDefault(x => x.IdKinologa == idKinologa);
                if (korisnik == null) return null;
                return ModelMapper.Map(korisnik);
            }
        }
    }
}